package com.michael.study.customview.animation

import android.animation.ValueAnimator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.animation.*
import com.michael.study.customview.R
import com.michael.study.customview.databinding.ActivityXmlAnimatinBinding

class CodeAnimationActivity : AppCompatActivity(), View.OnClickListener {

    private lateinit var mBinding:ActivityXmlAnimatinBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityXmlAnimatinBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        mBinding.btnScale.setOnClickListener(this)
        mBinding.btnAlpha.setOnClickListener(this)
        mBinding.btnTranslate.setOnClickListener(this)
        mBinding.btnRotate.setOnClickListener(this)
        mBinding.btnSet.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when(v?.id) {
            R.id.btn_scale -> {
                doScale()
            }
            R.id.btn_alpha -> {
                doAlpha()
            }
            R.id.btn_translate -> {
                doTranslate()
            }
            R.id.btn_rotate -> {
                doRotate()
            }
            R.id.btn_set -> {
                doSet()
            }
        }
    }

    private fun doScale() {
        var animation = ScaleAnimation(0.0f,1.2f,0.0f,1.2f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f)
        animation.duration = 1000
        animation.repeatCount = 3
        animation.interpolator = BounceInterpolator()
        mBinding.view.startAnimation(animation)
    }

    private fun doAlpha() {
        var animation = AlphaAnimation(0.0f,1.0f)
        animation.duration = 1000
        animation.repeatCount = 3
        animation.interpolator = BounceInterpolator()
        mBinding.view.startAnimation(animation)
    }

    private fun doTranslate() {
        var animation = TranslateAnimation(-100.0f,200.0f,-100.0f,200.0f)
        animation.duration = 1000
        animation.repeatCount = 3
        animation.interpolator = BounceInterpolator()
        mBinding.view.startAnimation(animation)
    }

    private fun doRotate() {
        var animation = RotateAnimation(0.0f,180.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f)
        animation.duration = 1000
        animation.repeatCount = 3
        animation.interpolator = BounceInterpolator()
        mBinding.view.startAnimation(animation)
    }

    private fun doSet() {
        var scaleAnimation = ScaleAnimation(0.0f,1.2f,0.0f,1.2f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f)
        var alphaAnimation = AlphaAnimation(0.0f,1.0f)
        var translateAnimation = TranslateAnimation(-100.0f,200.0f,-100.0f,200.0f)
        var rotateAnimation = RotateAnimation(0.0f,180.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f)

        var animationSet = AnimationSet(true)
        animationSet.duration = 3000
        animationSet.addAnimation(alphaAnimation)
        animationSet.addAnimation(scaleAnimation)
        animationSet.addAnimation(translateAnimation)
        animationSet.addAnimation(rotateAnimation)
        mBinding.view.startAnimation(animationSet)
    }
}